const foodsLoaclKey = 'foods'

export interface foodProps {
  id:number;
  name:string;
  img?:string;
  price:number;
  comment?:string;
}
const initArr:Array<foodProps> = [
  {id:1,name:'test',price:111,comment:'1213'},
  {id:2,name:'test2',price:111,comment:'1213'},
  {id:3,name:'test3',price:111,comment:'1213'},
]

export const setLoaclFood = (yourFood:foodProps) => {
  const newFoodsArr:Array<foodProps> = [...getLocalFood(), yourFood]
  window.localStorage.setItem(foodsLoaclKey, JSON.stringify(newFoodsArr))
}

export const getLocalFood = ():Array<foodProps> => {
  return JSON.parse(window.localStorage.getItem(foodsLoaclKey) as string)
}

export const getRandomLoaclFoods = (randomLength:number):Array<number> => {
  const beforeFoodsList = getLocalFood()
  const listLength = beforeFoodsList.length
  const res = []
  while(res.length<randomLength){
    let temp = Math.floor(Math.random()*100)
    if (temp < listLength){
      res.push(temp)
    }
  }
  return res
}

export const initLocalFood = () => {
  window.localStorage.setItem(foodsLoaclKey,JSON.stringify(initArr))
}

export const isHasLocalFood = ():any => {
  return !!window.localStorage.getItem(foodsLoaclKey)
}